//
//  WKOneNewViewController.m
//  WKDemo
//
//  Created by wangzhaohui-Mac on 14-8-6.
//  Copyright (c) 2014年 com.app. All rights reserved.
//

#import "WKFourNewViewController.h"
#import "WKVidelModel.h"
#import "WKVideoHttpTool.h"
#import "WKVideoParam.h"
#import "WKVideoResult.h"
#import "UIImageView+WebCache.h"
#import "WKCustomTableViewCell.h"
#import "WKVideoModelFrame.h"
#import "WKCommentViewController.h"

@interface WKFourNewViewController ()
@property (nonatomic,strong)NSMutableArray *datas;
@property (nonatomic,copy)NSString *currentMaxtime;

@end

@implementation WKFourNewViewController

- (NSMutableArray *)datas
{
    if (_datas == nil) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    
//    [WKVideoHttpTool getForwardNewDataListWithParam:nil success:^(WKVideoResult *result) {
//    
//        
//        self.datas = [NSMutableArray arrayWithArray:result.homeDatas];
//        //记录id
//        self.currentMaxtime = result.maxtime;
//        
//        [self.tableView reloadData];
//    } failure:^(NSError *error) {
//        WKLog(@"%@",error);
//    }];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //[self setupNavigationBarButtonItem];
    //添加下拉刷新控件
    [self.tableView addHeaderWithTarget:self action:@selector(loadNewData)];
    
    //添加上拉刷新控件
    [self.tableView addFooterWithTarget:self action:@selector(loadMoreData)];
    
}
//- (void)gotoCommentView:(NSNotification *)no
//{
//    WKLog(@"gotoCommentView");
//
//    WKCommentViewController *commentV = [[WKCommentViewController alloc] init];
//    commentV.videoF =  no.userInfo[@"videoF"];
//    [self.naVc pushViewController:commentV animated:YES];
//}
- (void)loadNewData
{
    
    [WKVideoHttpTool getForwardNewDataListWithParam:nil success:^(WKVideoResult *result) {
    
        
        self.datas = [NSMutableArray arrayWithArray:result.homeDatas];
        //记录id
        self.currentMaxtime = result.maxtime;
        
        [self.tableView reloadData];
        //显示底部刷新控件
        self.tableView.footerHidden = NO;
        [self.tableView headerEndRefreshing];
    } failure:^(NSError *error) {
        WKLog(@"%@",error);
        [self.tableView headerEndRefreshing];
    }];
    
}
- (void)loadMoreData
{
    WKLog(@"loadMoreData");
    WKVideoParam *param = [[WKVideoParam alloc] init];
    param.maxtime = self.currentMaxtime;
    WKLog(@"%@",param.maxtime);
    [WKVideoHttpTool getForwardMoreDataListWithParam:param success:^(WKVideoResult *result) {
   
        NSArray *array  = [NSMutableArray arrayWithArray:result.homeDatas];
        [self.datas addObjectsFromArray:array];
        
        //记录id
        self.currentMaxtime = result.maxtime;
        WKLog(@"%@",self.currentMaxtime);
        [self.tableView reloadData];
        [self.tableView footerEndRefreshing];
    } failure:^(NSError *error) {
        WKLog(@"%@",error);
        [self.tableView footerEndRefreshing];
    }
     
     ];
    
}
- (void)setupNavigationBarButtonItem
{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemImageName:@"navigationButtonPublish" highlightImage:@"navigationButtonPublishClick" target:self action:@selector(compile)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemImageName:@"navigationButtonRefresh" highlightImage:@"navigationButtonRefreshClick" target:self action:@selector(refresh)];
}

- (void)compile
{
    
    
}

- (void)refresh
{
    WKLog(@"eeeeee");
    
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WKCustomTableViewCell *cell = [WKCustomTableViewCell cellWithtableView:tableView];
    WKVideoModelFrame *result =self.datas[indexPath.row];
    cell.videoHeight = result.videoModel.height.floatValue;
    cell.videoFrame = result;
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WKVideoModelFrame *result =self.datas[indexPath.row];
    
    return result.cellHeight;
}
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(WKCustomTableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [cell removePlay];
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WKCommentViewController *commentV = [[WKCommentViewController alloc] init];
    commentV.videoF =  self.datas[indexPath.row];
    [self.naVc pushViewController:commentV animated:YES];
}

@end
